package com.example.swd1.gudong.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;

import com.example.swd1.gudong.R;

//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-9-13
//   Role :    None
//   Reference documents : None
//==================================================================================================================
public class LoadingThrid extends View {
    private Bitmap endBitmap;

    public LoadingThrid(Context context, AttributeSet attrs,
                                       int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public LoadingThrid(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LoadingThrid(Context context) {
        super(context);
        init();
    }

    private void init() {
        endBitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_loading_27));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureWidth(widthMeasureSpec)*endBitmap.getHeight()/endBitmap.getWidth());
    }

    private int measureWidth(int widthMeasureSpec){
        int result = 0;
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        }else {
            result = endBitmap.getWidth();
            if (mode == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);
            }
        }
        return result;
    }
}
